using UnityEngine;
using UnityEngine.Profiling;

namespace FutureCorePlugin
{
    public class AssistAppProfiler : MonoBehaviour
    {
        private GUIText viewText;

        private float deltaTime = 0f;
        private float updateTime = 1f;

        public float offsetX = 0;
        public float offsetY = 0;

        private int fontSize = 20;

        private void Start()
        {
            viewText = gameObject.AddComponent<GUIText>();
            viewText.color = Color.green;
            viewText.fontSize = fontSize;
            viewText.pixelOffset = new Vector2(offsetX, offsetY + viewText.fontSize);
        }

        private void Update()
        {
            deltaTime += Time.deltaTime;
            if (deltaTime >= updateTime)
            {
                string fps = (1.0f / Time.smoothDeltaTime).ToString("0");

                viewText.text =
                //FPS
                fps + "FPS"
                //总内存
                + " 总内存" + GetSize(Profiler.GetTotalReservedMemoryLong())
                //已占用内存
                + "-占用" + GetSize(Profiler.GetTotalAllocatedMemoryLong())
                //总Mono堆内存 (C#)
                + " 堆内存" + GetSize(Profiler.GetMonoHeapSizeLong())
                //已占用Mono堆内存 (C#)
                + "-占用" + GetSize(Profiler.GetMonoUsedSizeLong());

                deltaTime = 0f;
            }
        }

        private string GetSize(long length)
        {
            float size = Mathf.Floor(length / PMathConst.OneMbSize);
            string result = (int)size + "M";
            return result;
        }
    }
}